fn main() {
 
    // var_init_and_bind();
    var_scope();
   print_num(24);
   print_sum(5,6);

   let x = 5;
   println!("x is {}", x);
   println!("x add one is {}", add_one(x));

//    get_panic();

    let f:fn(i32)->i32 = add_one;
    println!("x add one is f {}", f(x));

    let f = add_one2;
    println!("x add one is f {}", f(x));

}
/// 使用系统出错 
fn get_panic() {
    panic!("this is the user error message!");
}

fn add_one(x: i32) -> i32 {
    x+1
}

fn add_one2(x: i32) -> i32 {
    return x+1;
}

fn print_sum(x:i32, y:i32) {
    println!("x + y = {}", x+y);
}

/// 打印输出x
fn print_num(x:i32) {
    println!("x is {}", x);
}

/// 变量作用范围
fn var_scope() {
    let x = 17;
    {
        let y = 33;
        println!("Inner The value of x is {} and value of y is {}", x, y);
    }
    // println!("Outter The value of x is {} and value of y is {}", x, y); // This won't work

    println!("当前x值：{}", x);
    {
        let x = 2;
        println!("内部x值：{}", x);
    }
    println!("出块后x值：{}", x);
    let x = 25;
    println!("出块后改变的x值：{}", x);
}


/// 
/// 变量赋值与绑定
/// 
fn var_init_and_bind()  {
       // 变更绑定
    let x = 5; // x:i32 默认类型
    println!("x binding to 5 is:{}", x);

    // 模式 批量变量绑定
    let (x, y) = (3, 7);
    println!("模式绑定的变量 x:{}, y:{}", x, y);

    // 类型注解（说明）
    let x:i32 = 5; //32位有符号整数  可能的大小是 8 16 32 64bit
    println!("声明i32 x  is:{}", x);

    // 变量默认是不可变的。
    // x=7;  //此行是一个编译错误

    // 可变变量
    let mut x = 4;
    println!("可变x初始值 x= {}", x);
    x = 7;
    println!("赋值后x= {}", x);

    
    //变量在使用前必须进行初始化
    // let a:i32; //如果后面没有使用代码只是一个警告，
    // println!("未初始化的 a= {}", a); //如果使用到变量a就报错了。
}
